<template>
  <div class="settings-container">
    <h2>{{ $t('accountSettings.accountSettings') }}</h2>
    <div class="settings-content">
      <!-- 修改密码部分 -->
      <div class="password-section">
        <h3>{{ $t('accountSettings.changePassword') }}</h3>
        <div class="password-form">
          <div class="form-item">
            <label>{{ $t('accountSettings.newPassword') }}</label>
            <input 
              type="password" 
              v-model="newPassword" 
              :placeholder="$t('accountSettings.newPasswordPlaceholder')"
              class="password-input"
            />
          </div>
          <div class="form-item">
            <label>{{ $t('accountSettings.confirmPassword') }}</label>
            <input 
              type="password" 
              v-model="confirmPassword" 
              :placeholder="$t('accountSettings.confirmPasswordPlaceholder')"
              class="password-input"
            />
          </div>
          <div class="form-actions">
            <button @click="changePassword" class="save-btn" :disabled="loading">
              {{ loading ? $t('accountSettings.saving') : $t('accountSettings.saveChanges') }}
            </button>
          </div>
        </div>
      </div>
      

    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { userApi } from '../../api/index.js'
import { useI18n } from 'vue-i18n'

// 使用i18n
const { t } = useI18n()

// 响应式数据
const newPassword = ref('')
const confirmPassword = ref('')
const loading = ref(false)

// 修改密码方法
const changePassword = async () => {
  if (!newPassword.value) {
    ElMessage.warning(t('accountSettings.pleaseEnterNewPassword'))
    return
  }
  
  if (newPassword.value !== confirmPassword.value) {
    ElMessage.error(t('accountSettings.passwordsDoNotMatch'))
    return
  }
  
  if (newPassword.value.length < 6) {
    ElMessage.warning(t('accountSettings.passwordTooShort'))
    return
  }
  
  loading.value = true
  try {
    // 调用修改密码的API
    await userApi.changePassword({
      new_password: newPassword.value,
      confirm_password: confirmPassword.value
    })
    
    ElMessage.success(t('accountSettings.passwordChangeSuccess'))
    newPassword.value = ''
    confirmPassword.value = ''
  } catch (error) {
    console.error('密码修改失败:', error)
    ElMessage.error(error.response?.data?.message || t('accountSettings.passwordChangeFailed'))
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.settings-container {
  width: 100%;
  height: 100%;
  padding: 30px;
  overflow-y: auto;
  background: transparent;
}

.settings-container h2 {
  margin-bottom: 30px;
  color: #333;
  font-size: 1.5rem;
  font-weight: 600;
}

.settings-content {
  display: flex;
  flex-direction: column;
  gap: 30px;
}

/* 密码修改部分 */
.password-section {
  background: #ffffff;
  border-radius: 8px;
  padding: 30px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  border: 1px solid #e0e0e0;
}

.password-section h3 {
  margin-bottom: 20px;
  color: #333;
  font-size: 1.2rem;
  font-weight: 600;
}

.password-form {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.form-item {
  display: flex;
  flex-direction: column;
  gap: 8px;
  align-items: center;
}

.form-item label {
  font-weight: 500;
  color: #333;
  font-size: 14px;
  text-align: center;
}

.password-input {
  padding: 12px 16px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 14px;
  background: #fff;
  transition: border-color 0.3s ease;
  width: 100%;
  max-width: 300px;
}

.password-input:focus {
  outline: none;
  border-color: #409eff;
  box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.1);
}

.form-actions {
  margin-top: 10px;
}

.save-btn {
  background: #409eff;
  color: white;
  border: none;
  padding: 12px 24px;
  border-radius: 4px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

.save-btn:hover:not(:disabled) {
  background: #337ecc;
}

.save-btn:active:not(:disabled) {
  background: #2d6da3;
}

.save-btn:disabled {
  background: #c0c4cc;
  cursor: not-allowed;
}


</style> 